﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;

namespace SHomeWorkshop.LunarPresentation.Widegets
{
    /// <summary>
    /// 公式被折叠后显示的折叠文本部件。
    /// 基本形状是“被折叠公式类型◆备注文本（如果有的话）”。
    /// </summary>
    public class ExCollapseMark : Border
    {
        public ExCollapseMark(Expressions.Expression masterEx)
        {
            this.masterEx = masterEx;

            this.BorderThickness = defBorderWidth;

            if (masterEx != null)
            {
                if (masterEx.BorderWidth > 1 &&
                    masterEx.BorderColor != Brushes.Transparent)
                {
                    this.BorderThickness = defNoBorderWidth;
                }
            }

            this.mainTextBlock = new TextBlock()
            {
                Margin = margin,
            };

            this.mainTextBlock.MouseLeftButtonDown +=
                new System.Windows.Input.MouseButtonEventHandler(mainTextBlock_MouseLeftButtonDown);
            this.mainTextBlock.MouseLeftButtonUp +=
                new System.Windows.Input.MouseButtonEventHandler(mainTextBlock_MouseLeftButtonUp);

            this.Child = mainTextBlock;
        }

        private static Thickness defBorderWidth = new Thickness(2);

        private static Thickness defNoBorderWidth = new Thickness(0);

        void mainTextBlock_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (this.masterEx != null)
            {
                Globals.DropExpression(this.masterEx);
            }
        }

        void mainTextBlock_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (this.masterEx != null &&
                this.masterEx.Editor != null)
            {
                Globals.dragSourceExpression = this.masterEx;
            }
        }

        public static Thickness margin = new System.Windows.Thickness(6);

        private TextBlock mainTextBlock;

        private Expressions.Expression masterEx;

        private Span exNameSpan = null;

        private Span exCommentSpan = null;

        private Span start = null;

        private Span mid = null;

        private Span end = null;

        public void RefreshExCollpseMarkText()
        {
            this.mainTextBlock.Inlines.Clear();

            if (masterEx != null)
            {
                if (start == null)
                {
                    start = new Span(new Run("◇【折叠】－"));
                }
                this.mainTextBlock.Inlines.Add(start);

                if (exNameSpan == null)
                {
                    exNameSpan = new Span(new Run(masterEx.ChineseName))
                        {
                            FontWeight = FontWeights.Bold
                        };
                }
                else
                {
                    exNameSpan.Inlines.Clear();
                    exNameSpan.Inlines.Add(new Run(masterEx.ChineseName));
                }

                this.mainTextBlock.Inlines.Add(exNameSpan);

                if (masterEx.CommentText != null && masterEx.CommentText.Length > 0)
                {
                    if (mid == null)
                    {
                        mid = new Span(new Run("◆"));
                    }
                    this.mainTextBlock.Inlines.Add(mid);

                    if (this.exCommentSpan == null)
                    {
                        this.exCommentSpan = new Span(new Run(masterEx.CommentText));
                    }
                    else
                    {
                        this.exCommentSpan.Inlines.Clear();
                        this.exCommentSpan.Inlines.Add(new Run(masterEx.CommentText));
                    }

                    this.mainTextBlock.Inlines.Add(exCommentSpan);
                }

                if (end == null)
                {
                    end = new Span(new Run("◇"));
                }
                this.mainTextBlock.Inlines.Add(end);

                UpdateForeground();
            }
        }

        internal void UpdateForeground()
        {
            if (this.masterEx != null && this.masterEx.Editor != null)
            {
                if (this.exNameSpan != null)
                {
                    this.exNameSpan.Foreground = this.masterEx.Editor.DefaultForeground;
                }

                if (this.exCommentSpan != null)
                {
                    this.exCommentSpan.Foreground = this.masterEx.Editor.DefaultForeground;
                }

                if (start != null)
                {
                    start.Foreground = this.masterEx.Editor.DefaultForeground;
                }

                if (mid != null)
                {
                    mid.Foreground = this.masterEx.Editor.DefaultForeground;
                }

                if (end != null)
                {
                    end.Foreground = this.masterEx.Editor.DefaultForeground;
                }

                if (masterEx.BorderWidth > 1 &&
                    masterEx.MainBorder.BorderBrush != Brushes.Transparent)
                {
                    this.BorderThickness = defNoBorderWidth;
                }
                else
                {
                    this.BorderThickness = defBorderWidth;
                }

                this.BorderBrush = this.masterEx.Editor.DefaultForeground;
            }
        }
    }
}
